package com.mty.mydemo.act;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import com.litesuits.common.utils.InputMethodUtils;
import com.litesuits.sdk.act.LiteActivity;
import com.litesuits.sdk.views.CustomDialog;
import com.mty.mydemo.View.CustomTipsView;
import com.qunduikj.sjb.sdk.model.enums.MobEvent;
import com.umeng.analytics.MobclickAgent;

/**
 * @author MaTianyu on 2015-05-05
 */
public abstract class BaseActivity extends LiteActivity implements SupportInterface {
    protected boolean containsFragment;

    protected void onResume() {
        super.onResume();
        logi("onResume containsFragment: " + containsFragment);
        if (!containsFragment) {

            MobclickAgent.onPageStart(TAG); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        }
        MobclickAgent.onResume(this);          //统计时长
    }

    protected void onPause() {
        super.onPause();
        logi("onPause containsFragment: " + containsFragment);
        if (!containsFragment) {
            // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
            MobclickAgent.onPageEnd(TAG);
        }
        //统计时长
        MobclickAgent.onPause(this);
        InputMethodUtils.hideSoftInput(activity);
    }

    /* ________________ fragment hack ________________ */

    @Override
    public void onBackPressed() {
        try {
            super.onBackPressed();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setContainsFragment(boolean has) {
        this.containsFragment = has;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //默认隐藏软件盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    @Override
    public CustomTipsView createTipsView() {
        CustomTipsView tipsView = new CustomTipsView(activity);
        tipsView.setNoNetworkDetail(0, 0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(activity, MobEvent.NetRetry.getEventID());//点击无网络重试
                loadDataStart();
            }
        });
        return tipsView;
    }


    public void setTitleBackground(int resID) {
        getDefaultTitleBar().getTvTitle().setBackgroundResource(resID);
    }

    public void setTitleText(int resID) {
        getDefaultTitleBar().getTvTitle().setText(resID);
    }


    /**
     * 设置title中左边的图片
     * 使用场合：非返回图片调用
     * 注：title的左边的返回键默认已经有监听了，不需要单独定义
     */
    public void setLeftImage(int resID) {
        if (getDefaultTitleBar().hasTitlebar()) {
            getDefaultTitleBar().setLeftImage(resID);
        }
    }


    /**
     * 编辑框的删除键
     *
     * @param ET_resID 编辑框的id
     * @param IV_resID 删除按钮的id
     */
    public void setEvDel(int ET_resID, int IV_resID) {
        final EditText editText = (EditText) findViewById(ET_resID);
        final ImageView delText = (ImageView) findViewById(IV_resID);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.length() > 0) {
                    delText.setVisibility(View.VISIBLE);
                } else {
                    delText.setVisibility(View.GONE);
                }
            }

        });
        delText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("");
            }
        });
    }

    /**
     * 代替toast的提示框
     *
     * @param msg
     */
    public void showDialogTip(String msg) {
        showDilogTip(msg, 1500);
    }

    public void showDilogTip(String msg, long m) {
        final Dialog dialog = buildDialog(msg).create();
        dialog.show();
        getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, m == 0 ? 1500 : m);
    }

    /**
     * 简单的确认提示框构造器
     *
     * @param content
     * @return
     */
    public CustomDialog.Builder buildDialog(String content) {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage(content);
        return builder;
    }

}
